* Economic Risk within the Household and Voting for the Radical Right
* Tarik Abou-Chadi and Thomas Kurer
* World Politics

** REPLICATION FILES

* Part 1: Calculate Occupational Risk in EU-SILC
* by ISCO 1-digit and ISCO 2-digit

* Using the following versions of EU-SILC:
* UDB_c04P_ver 2004-4 from 01-08-09
* UDB_c06P_ver 2006-4 from 01-03-10
* UDB_c08P_ver 2008-7 from 01-03-15
* UDB_c10P_ver 2010-6 from 01-03-15
* UDB_c12P_ver 2012-5 from 01-03-17
* UDB_c14P_ver 2014-3 from 01-03-17


* PREPARATION

clear
file close _all
macro drop _all

set more off

* set paths (needs to be adjusted before running script)
global path "..."
global final "..."


set more off

* MERGE EU-SILC FILES
* run this only once

/*
* converting csv to dta
cd "$path/original"
local myfilelist : dir . files"*.csv"
foreach file of local myfilelist {
drop _all
insheet using "`file'"
local outfile = subinstr("`file'",".csv","",.)
save "$path/original/`outfile'", replace
}


* Rename waves

local myfilelistdta : dir . files"*.dta"
foreach file of local myfilelistdta {
local wave = substr("`file'",6,3)
use "`file'"
save "$path/modified/silc`wave'.dta", replace
}

* Then loop through them to create additional variable missing in earlier waves of SILC 
* working status changed in 2008 (PL030 to PL031), ISCO08 has been introduced in 2009 (PL050 to PL051)

* Allows for easy appending below

cd "$path/modified"

* Manually create filelist by typing in filenames
local myfilelist06 silc04P.dta silc06P.dta
* add variable PL111, which only enters SILC from 2008 onwards
foreach filename of local myfilelist06 {
  use "`filename'"
  gen pl111 = ""
  save "`filename'", replace
}

* Manually create filelist by typing in filenames
local myfilelist08 silc04P.dta silc06P.dta silc08P.dta
* add variable PL031, which only enters SILC from 2009 onwards
* add variable PL051, which only enters SILC from 2010 onwards
foreach filename of local myfilelist08 {
  use "`filename'"
  gen pl031 = .
  gen pl051 = .
  save "`filename'", replace
}


* MERGING
* append different waves

* create a txt.file that entails all the files to merge
! ls *.dta >filelist.txt

* reduce data to relevant variables, append waves


file open myfile using "filelist.txt", read
file read myfile line

use `line'
save "$path/SILC_pooled.dta", replace

file read myfile line
while r(eof)==0 { /* while you're not at the end of the file */
	append using `line'
	keep pl040 pl050 pl051 pl030 pl031 pl060 pl100 pl110 pl111 pl140 pl120 pb020 pb010 pb030 pb140 pb150 py010n py020n py010g py020g ph010 ph060 ph070 pl160 pl170 pe040 pl130 pl150
	file read myfile line
}
file close myfile

* check duplicates
duplicates report _all

* SAVE POOLED FILE
save "$path/SILC_pooled.dta", replace

*/


* READ POOLED EU-SILC FILE

clear
use "$path/SILC_pooled.dta"

* GENERATE AND RECODE VARIABLES

gen year = pb010
gen cntry = pb020
gen id = pb030


** Age

gen yearbirth = pb140
gen age = year-yearbirth

** Education

gen educ_level = pe040
recode educ_level (0/2=1)(3/4=2)(5/6=3)
recode educ_level (100/299=1)(300/499=2)(500/800=3) /* new coding from 2014 onwards */


label define educ_level  ///
1 "Less than upper secondary education" ///
2 "Upper secondary education" ///
3 "Tertiary education"
label values educ_level  educ_level 

tab pe040 educ_level , m

gen isced = pe040
recode isced (0=1) (100=1) (200=2) (300/399=3) (400/499=4) (500=5) (6=5)

label define isced ///
1 "primary education" ///
2 "lower secondary education" ///
3 "(upper) secondary education" ///
4 "post-secondary non tertiary" ///
5 "tertiary education", replace
label values isced isced
label variable isced "Highest ISCED level attained"

** Self-Employment (following Oesch class scheme code)

gen selfemp_level  = .
* not self-employed (employee or family worker)
replace selfemp_level  = 1 if pl040==3 | pl040==4

* self-employed without employees
replace selfemp_level  = 2 if pl040==2

* self-employment with less than 10 employees (not directly applicable in SILC): self-em with employees, number of workers at local unit<10
replace selfemp_level  = 3 if pl040==1& (inrange (pl130, 1,10))
replace selfemp_level  = 3 if pl040==1& pl130==14

* self-employed with more than 10 employees: self-em with employees, number of workers at local unit>10 (pl130== 11, 12, 13, 15) 

replace selfemp_level  = 4 if pl040==1&(inrange (pl130, 11, 13))
replace selfemp_level  = 4 if pl040==1& pl130==15

label define selfemp_level  ///
1 "Not self-employed" ///
2 "Self-empl without employees" ///
3 "Self-empl with 1-9 employees" ///
4 "Self-empl with 10 or more"
label values selfemp_level  selfemp_level 

** Occupation and Industry Codes

gen isco88 = pl050
gen isco08 = pl051

gen nace = pl110
gen nace2 = pl111

** Female

gen female = pb150-1

** Employment Status
gen empstat = pl030

* new variable coding from pl031 on. harmonize.
replace empstat = 1 if pl031==1 | pl031==3
replace empstat = 2 if pl031==2 | pl031==4
replace empstat = 3 if pl031==5
replace empstat = 4 if pl031==6
replace empstat = 5 if pl031==7
replace empstat = 6 if pl031==8
replace empstat = 7 if pl031==9
replace empstat = 8 if pl031==10
replace empstat = 9 if pl031==11

label define empstat  ///
1 "full-time" ///
2 "part-time" ///
3 "unemp" ///
4 "pupil,student" ///
5 "retired" ///
6 "disabled" ///
7 "military" ///
8 "domestic tasks" ///
9 "other inactive"
label values empstat empstat

gen unemp = (empstat==3)
replace unemp = . if empstat==.

gen part = (empstat==2)
replace part = . if empstat==.

** Income

gen ninc = py010n
label values ninc ninc
label variable ninc "Employee Cash or Near-Cash Income (Net)"

gen nincnc = py020n
label values nincnc nincnc
label variable nincnc "Non-Cash Employee Income (Net)"

gen ginc = py010g
label values ginc ginc
label variable ginc "Employee Cash or Near-Cash Income (Gross)"

gen gincnc = py020g
label values gincnc gincnc
label variable gincnc "Non-Cash Employee Income (Gross)"

** Auxiliary Variables

gen nrhours = pl060
gen nrhoursother = pl100
gen nrhourstotal = pl060 + pl100

gen reasonhours = pl120
label define reasonhours ///
1 "education" ///
2 "ill/disabled" ///
3 "want to but cannot find" ///
4 "do not want more" ///
5 "nr considered fulltime" ///
6 "housework" ///
7 "other", replace
label values reasonhours reasonhours
label variable reasonhours "Reason for working <30 hours"

gen involpart = part
replace involpart = 0 if reasonhours==4

gen temp = (pl140==2)
replace temp=. if pl140==.

gen health = ph010
label define health ///
1 "very good" ///
2 "good" ///
3 "fair" ///
4 "bad" ///
5 "very bad", replace
label values health health
label variable health "General Health"

gen changejob = pl160
replace changejob=0 if pl160==2
label values changejob changejob
label variable changejob "Change of Job since last Year (1=yes)"

gen changereason = pl170
label define changereason ///
1 "To take up or seek better job" ///
2 "End of temporary contract" ///
3 "Obliged to stop by employer"  ///
4 "Sale or closure of own/family business" ///
5 "Child care and care for other dependent" ///
6 "Partner’s job required us to move / marriage" ///
7 "Other reasons", replace
label values changereason changereason
label variable changereason "Reason for Changing Jobs"

gen managerial = pl150
replace managerial = 0 if pl150 ==2
label values managerial managerial
label variable managerial "Managerial position (1=yes)"


** Generate Oesch Class

gen class16=.

* First allocate employed respondents to schema
* Allocate self-emp at the end to avoid missings due to missings in selfemp

* ISCO-88

* isco = 1, armed forces
replace class16=10 if year<2011 & isco88 == 1

* isco = 11, senators, legislators, senior officials
* isco = 12, corporate managers
replace class16=5 if year<2011 & inrange(isco88 ,11, 12)

* isco = 13, general managers
replace class16=6 if year<2011 & isco88 ==13

* isco = 21, physical, mathematician, engineering science professionals
replace class16=9 if year<2011 & isco88 ==21

* isco = 22 
replace class16=9 if year<2011 & isco88 ==22
* plus isco = 22 and health --> sociocultural
replace class16=13 if year<2011 & isco88 ==22 & nace=="m"
replace class16=13 if year<2011 & isco88 ==22 & nace=="n"

* isco = 23 --> teacher. all socio cultural. primary teachers are semi-profs
 
replace class16=13 if year<2011 & isco88 ==23
replace class16=14 if year<2011 & isco88 ==23 & educ_level !=3

* isco = 24 --> other professionals
replace class16=14 if year<2011 & isco88==24
* inner sociocultural division via education
replace class16=13 if year<2011 & isco88==24 & educ_level==3

* 2410-2429 = business, accountants and lawyers
replace class16=5 if year<2011 & isco88 ==24 & nace=="j"
replace class16=5 if year<2011 & isco88 ==24 & nace=="k"
* replace managerial position within isco==24 as (associate) managers
replace class16=5 if year<2011 & isco88 ==24 & pl150==1

* isco = 31, technicians and associate professionals
replace class16=10 if year<2011 & isco88 ==31

* isco = 32, life science and health associate profs. difficult to separate. use sector and education in addition.
* mostly soc.cul semi-prof
replace class16=10 if year<2011 & isco88 ==32
* some assistant jobs in health are skilled services rather than socio-cult. sem profs. 
* some technicians (life science, agronomy). 
replace class16=14 if year<2011 & nace=="m" & isco88 ==32
replace class16=14 if year<2011 & nace=="n" & isco88 ==32
replace class16=14 if year<2011 & nace=="o" & isco88 ==32
* separate via education (lower secondary at max)
replace class16=15 if year<2011 & pe040==0 & isco88 ==32
replace class16=15 if year<2011 & pe040==1 & isco88 ==32
replace class16=15 if year<2011 & pe040==2 & isco88 ==32

* isco = 33, teaching associate profs
replace class16=14 if year<2011 & isco88 ==33

* isco = 34, other associate profs
replace class16=14 if year<2011 & isco88 ==34
* 3411-3429, business, financial
replace class16=6 if year<2011 & isco88 ==34 & nace == "j"
replace class16=6 if year<2011 & isco88 ==34 & nace == "k"

* replace managerial position within isco==34 as (associate) managers
replace class16=6 if year<2011 & isco88 ==34 & pl150==1

* isco = 41, 42: office clerks, customer service clerks
replace class16=7 if year<2011 & educ_level !=1 & inrange(isco88 ,40, 42)
replace class16=8 if year<2011 & educ_level ==1 & inrange(isco88 ,40, 42)

* isco 51, 52: personal service workers, salespersons, ...
replace class16=15 if year<2011 & educ_level !=1 & inrange(isco88 ,50, 52)
replace class16=16 if year<2011 & educ_level ==1 & inrange(isco88 ,50, 52)

* isco 61: skilled agricultural and fishery
replace class16=11 if year<2011 & educ_level !=1 & isco88 ==61
replace class16=12 if year<2011 & educ_level ==1 & isco88 ==61

* isco 7*: craft and related trade workers
replace class16=11 if year<2011 & educ_level !=1 & inrange(isco88 ,70, 74)
replace class16=12 if year<2011 & educ_level ==1 & inrange(isco88 ,70, 74)

* isco 8*: plant and machine operators
replace class16=11 if year<2011 & educ_level !=1 & inrange(isco88 ,80, 83)
replace class16=12 if year<2011 & educ_level ==1 & inrange(isco88 ,80, 83)

* isco 91: service elementary
replace class16=16 if year<2011 & isco88 ==91

* replace 91 = 12 if manufacturing, construction, agriculture
replace class16=12 if year<2011 & isco88 ==91 & inlist(nace,"a", "b", "c", "d", "f")


* isco 92, 93: agriculture, fishing, labouring, manufacturing, construction
replace class16=12 if year<2011 & isco88 ==92
replace class16=12 if year<2011 & isco88 ==93

* Allocate Self-Emp
* analoguous to Oesch code

replace class16=1 if selfemp_level ==4 & year<2011
replace class16=2 if inlist(selfemp_level ,2,3) & inrange(isco88 ,20,24) & year<2011
replace class16=3 if selfemp_level ==3 & year<2011 ///
 & (inrange(isco88 ,1,19) | inrange(isco88 ,30,93))
replace class16=4 if selfemp_level ==2 & year<2011 ///
 & (inrange(isco88 ,1,19) | inrange(isco88 ,30,93))

* ISCO-08

* isco = 1-3, armed forces
replace class16=10 if year>2011 & inlist(isco08, 1, 2, 3)

* isco = 11, chief executives etc.
* isco = 12, admin and commercial managers
replace class16=5 if year>2011 & inrange(isco08 ,11, 12)

* isco = 13, service managers
* isco = 14, hospitaliry and other managers
replace class16=6 if year>2011 & inrange(isco08 ,13, 14)

* isco = 21, science and engineering professionals
replace class16=9 if year>2011 & isco08 ==21

* isco = 22, health professionals --> sociocultural
replace class16=13 if year>2011 & isco08 ==22

* isco = 23 --> teacher. all socio cultural. primary teachers are semi-profs
 
replace class16=13 if year>2011 & isco08 ==23
replace class16=14 if year>2011 & isco08 ==23 & educ_level !=3

* isco = 24 --> business and admin professionals
replace class16=6 if year>2011 & isco08==24
* replace managerial position within isco==24 as (associate) managers
replace class16=5 if year>2011 & isco08 ==24 & pl150==1

* isco 25, information and communication technology
replace class16 = 9 if year>2011 & isco08 ==25

* isco 26, legal, social and cultural professionals
replace class16 = 13 if year>2011 & isco08 ==26

* isco = 31, science and engineering
replace class16=9 if year>2011 & isco08 ==31

* isco = 32,  health associate profs
* mostly soc.cul semi-prof
replace class16=14 if year>2011 & isco08 ==32
* some assistant jobs in health are rather skilled services than socio-cult. sem profs. 

* separate via education (lower secondary at max).
replace class16=15 if year>2011 & pe040==0 & isco08 ==32
replace class16=15 if year>2011 & pe040==1 & isco08 ==32
replace class16=15 if year>2011 & pe040==2 & isco08 ==32

* isco = 33, business and admin assoc profs
replace class16=6 if year>2011 & isco08 ==33
replace class16=5 if year>2011 & isco08 ==33 & pl150==1

* isco = 34, legal, social, cultural related assoc profs
* eg. legal/social/religious assoc prof, sports and fitness, artistic, cultural
replace class16=14 if year>2011 & isco08 ==34
* here more clearly differentiated than isco88, which mixed 33 and 34

* isco = 35, information and communication technicians
replace class16=10 if year>2011 & isco08 ==35

* isco = 41, 42: office clerks, customer service clerks
* isco = 43, 44: numerical and recording clerks, other clerical support workers
replace class16=7 if year>2011 & educ_level !=1 & inrange(isco08 ,40, 43)
replace class16=8 if year>2011 & educ_level ==1 & inrange(isco08 ,40, 43)
replace class16=8 if year>2011 & isco08 == 44

* isco 51, 52: personal service workers, salespersons, ...
* isco 53 personal care, isco 54 protective service
replace class16=15 if year>2011 & educ_level !=1 & inrange(isco08 ,50, 54)
replace class16=16 if year>2011 & educ_level ==1 & inrange(isco08 ,50, 54)

* isco 61: market oriented skilled agricultural work
* isco 62, market oriented skilled forestry, fishery
* isco 63 subsistence farmer, fisher, etc.
replace class16=11 if year>2011 & educ_level !=1 & inrange(isco08 ,60, 62)
replace class16=12 if year>2011 & educ_level ==1 & inrange(isco08 ,60, 62)
replace class16=12 if year>2011 & isco08==63

* isco 7*: craft and related trade workers

replace class16=11 if year>2011 & educ_level !=1 & inrange(isco08 ,70, 75)
replace class16=12 if year>2011 & educ_level ==1 & inrange(isco08 ,70, 75)

* isco 8*: plant and machine operators

replace class16=11 if year>2011 & educ_level !=1 & inrange(isco08 ,80, 83)
replace class16=12 if year>2011 & educ_level ==1 & inrange(isco08 ,80, 83)

* isco 9x: elementary occupations

* isco 91: cleaner and helper
replace class16=16 if year>2011 & isco08 ==91

* isco 92, 93: agriculture, fishing; labouring, manufacturing, construction
replace class16=12 if year>2011 & isco08 ==92
replace class16=12 if year>2011 & isco08 ==93

* isco 94 food preparation assistant, isco 95 street and related sales
* lowskilled services
replace class16=16 if year>2011 & inrange(isco08, 94, 95)
* isco 96 elementary workers
replace class16=12 if year>2011 & isco08==96

* Allocate Self-Emp
* analoguous Oesch

replace class16=1 if selfemp_level ==4 & year>2011
replace class16=2 if inlist(selfemp_level ,2,3) & inrange(isco08 ,20,24) & year>2011
replace class16=3 if selfemp_level ==3 & year>2011 ///
 & (inrange(isco08 ,1,19) | inrange(isco08 ,30,93))
replace class16=4 if selfemp_level ==2 & year>2011 ///
 & (inrange(isco08 ,1,19) | inrange(isco08 ,30,93))
 
 
label define class16 ///
1 "Large employers (10 or more employees)" ///
2 "Self-employed professionals" ///
3 "Small business owners with 1-9 employees" ///
4 "Small business owners without employees" ///
5 "Managers and administrators" ///
6 "Associate managers and administrators" ///
7 "Skilled clerks" ///
8 "Routine clerks" ///
9 "Technical experts" ///
10 "Technicians" ///
11 "Skilled craft workers" ///
12 "Routine operatives" ///
13 "Socio-cultural professionals" ///
14 "Socio-cultural semi-professionals" ///
15 "Skilled service workers" ///
16 "Routine service workers"
label values class16 class16
tab class16

* Generate scheme with 8 classes

recode class16 (1/2=1)(3/4=2)(5/6=3)(7/8=4)(9/10=5)(11/12=6)(13/14=7)(15/16=8), gen(class8)

label variable class8 class8
label define class8 ///
1 "Self-employed professionals and large employers" ///
2 "Small business owners" ///
3 "(Associate) managers and administrators" ///
4 "Office clerks" ///
5 "Technical professionals and technicians" ///
6 "Production workers" ///
7 "Socio-cultural (semi-)professionals" ///
8 "Service workers"

label values class8 class8

tab class8

* ISCO88: not precise enough. isco 24 and 34: soc. cult. and associate managers mixed
* in order to differentiate between soc.cult. and associate managers, replace as manager if respondents has 10 or more employees
replace class8=3 if class8==7&isco88==24&selfemp_lev==1&pl150==1&inrange(pl130, 10, 13)
replace class8=3 if class8==7&isco88==24&selfemp_lev==1&pl150==1&pl130==15
replace class8=3 if class8==7&isco88==34&selfemp_lev==1&pl150==1&inrange(pl130, 10, 13)
replace class8=3 if class8==7&isco88==34&selfemp_lev==1&pl150==1&pl130==15

tab class8


* Alternative: Collapsed class scheme Kitschelt-Rehm

/* to generate the socio-economic class5 */
recode class16 (1/2=1)(5=1)(9=1)(13=1)(6=2)(10=2)(14=2)(3/4=3)(7=4)(11=4)(15=4)(8=5)(12=5)(16=5), gen(class5)

label var class5 "Socio-economic class5"


label define class5  1 "Higher-grade service class" 2 "Lower-grade service class" 3 "Small business owners" 4 "Skilled workers" 5 "Unskilled workers"
label values class5 class5

* Generate various baseline groups for calculation of occupational unemployment risk

* primary group: isco-1d and isco-2d

gen isco88_2d = isco88
gen isco08_2d = isco08

gen isco88_1d = real(substr(string(isco88, "%5.0g"), 1, 1))
gen isco08_1d = real(substr(string(isco08, "%5.0g"), 1, 1))

* baseline with socio-demographic subgrouping

gen young=.
replace young=1 if age<40
replace young=0 if age>39

label define young 0 "old" 1 "young"
label values young young

label define female 0 "male" 1 "female"
label values female female

gen class8short = class8
label variable class8short class8short
label define class8short ///
1 "Self-emp prof/employers" ///
2 "Small business owners" ///
3 "Managers/Admin" ///
4 "Office clerks" ///
5 "(Prof) Technicians" ///
6 "Production workers" ///
7 "Socio-cult spec" ///
8 "Service workers"

label values class8short class8short

egen group = group(class8short young female)

egen lab = concat(young female class8short), decode p(" ")
* ssc install labutil
labmask group, val(lab)
drop lab class8short

egen isco88group = group(isco88_1d young female)

egen labisco88 = concat(young female isco88_1d), decode p(" ")
labmask isco88group, val(labisco88)

egen isco08group = group(isco08_1d young female)

egen labisco08 = concat(young female isco08_1d), decode p(" ")
labmask isco08group, val(labisco08)


* DEFINE FINAL SAMPLE FOR RISK CALCULATION

* only active labor force as sample
drop if empstat>3

* country rates
bysort cntry year: egen unempcy = mean(unemp)

* group rates
bysort cntry year group: egen unempgr = mean(unemp)

* replace very small groups (unreliable risk measures) with less disaggregated class8 groups

gen one = 1
bysort cntry year group: egen ngroup = count(one)
bysort cntry year class8: egen nclass8 = count(one)

bysort cntry year class8: egen unempo8 = mean(unemp)

gen unemprisk = unempgr
replace unemprisk = unempo8 if ngroup<50 & nclass>200
replace unemprisk = . if nclass<50
replace unemprisk = . if class8==.

* replace zeros (e.g. FI 2004, NO2014: unemp status only for non-missing isco)
bysort cntry year: egen sumunemp=max(unemp) if group!=.

tab cntry year if sumunemp==0

replace unemprisk=. if sumunemp==0

lab var unemprisk "Unemprisk: share of unemp per group32, share of class8 if ngroup<50"

* by ISCO groups (main specification)

bysort cntry year isco88_1d: egen unempisco88_1d = mean(unemp)
bysort cntry year isco08_1d: egen unempisco08_1d = mean(unemp)
bysort cntry year isco88_2d: egen unempisco88_2d = mean(unemp)
bysort cntry year isco08_2d: egen unempisco08_2d = mean(unemp)

replace unempisco88_1d=. if isco88_1d==.
replace unempisco08_1d=. if isco08_1d==.
replace unempisco88_2d=. if isco88_2d==.
replace unempisco08_2d=. if isco08_2d==.

* replace very small isco 2d groups (unreliable risk measures) with less disaggregated isco 1d groups

bysort cntry year isco88_2d: egen nisco88_2d = count(one) if unemp!=.
bysort cntry year isco08_2d: egen nisco08_2d = count(one) if unemp!=.

gen unempisco88_2d_robust = unempisco88_2d
replace unempisco88_2d_robust = unempisco88_1d if nisco88_2d<50
replace unempisco88_2d_robust = . if isco88_2d==.

gen unempisco08_2d_robust = unempisco08_2d
replace unempisco08_2d_robust = unempisco08_1d if nisco08_2d<50
replace unempisco08_2d_robust = . if isco08_2d==.

* replace zeros (e.g. FI 2004, NO2014: unemp status only for non-missing isco)
replace unempisco88_1d=. if sumunemp==0
replace unempisco08_1d=. if sumunemp==0
replace unempisco88_2d=. if sumunemp==0
replace unempisco08_2d=. if sumunemp==0


* by ISCO groups

bysort cntry year isco88group: egen unempisco88gr = mean(unemp)
bysort cntry year isco08group: egen unempisco08gr = mean(unemp)
* gr missings will be replaced below


bysort cntry year isco88group: egen nisco88group = count(one)
bysort cntry year isco88_1d: egen nisco88_1d = count(one)

gen unemprisk_isco88 = unempisco88gr
replace unemprisk_isco88 = unempisco88_1d if nisco88group<50 & nisco88_1d>200
replace unemprisk_isco88 = . if nisco88_1d<50
replace unemprisk_isco88 = . if isco88_1d==.

* replace zeros (e.g. FI 2004, NO2014: unemp status only for non-missing isco)
replace unemprisk_isco88=. if sumunemp==0

lab var unemprisk_isco88 "Unemprisk: share of unemp per isco88_group, share of isco88_1d if nisco88_group<50"

bysort cntry year isco08group: egen nisco08group = count(one)
bysort cntry year isco08_1d: egen nisco08_1d = count(one)

gen unemprisk_isco08 = unempisco08gr
replace unemprisk_isco08 = unempisco08_1d if nisco08group<50 & nisco08_1d>200
replace unemprisk_isco08 = . if nisco08_1d<50
replace unemprisk_isco08 = . if isco08_1d==.

* replace zeros (e.g. FI 2004, NO2014: unemp status only for non-missing isco)
replace unemprisk_isco08=. if sumunemp==0


lab var unemprisk_isco08 "Unemprisk: share of unemp per isco08_group, share of isco08_1d if nisco08_group<50"

* By ISCO and gender

egen isco88female = group(isco88_1d female)

egen labisco88female = concat(female isco88_1d), decode p(" ")
labmask isco88female, val(labisco88female)

egen isco08female = group(isco08_1d female)

egen labisco08female = concat(female isco08_1d), decode p(" ")
labmask isco08female, val(labisco08female)

bysort cntry year isco88female: egen unempisco88female = mean(unemp)
bysort cntry year isco08female: egen unempisco08female = mean(unemp)

replace unempisco88female=. if isco88_1d==.
replace unempisco08female=. if isco08_1d==.


* replace zeros (e.g. FI 2004, NO2014: unemp status only for non-missing isco)
replace unempisco88female=. if sumunemp==0
replace unempisco08female=. if sumunemp==0

*keep cntry year class8 group isco88_1d isco88_2d isco08_1d isco08_2d isco88group isco08group outcy unempcy tempcy partcy risk risk_std unemprisk temprisk partrisk unemprisk_isco88 unemprisk_isco08 unempisco88_2d unempisco08_2d unempisco88_1d unempisco08_1d
keep cntry year class8 group isco88_1d isco88_2d isco08_1d isco08_2d isco88group isco08group isco88female isco08female unempcy unemprisk unemprisk_isco88 unemprisk_isco08 unempisco88_2d unempisco08_2d unempisco88_2d_robust unempisco08_2d_robust unempisco88_1d unempisco08_1d unempisco88female unempisco08female

*===============================================================================
* fix some issues for merging
*===============================================================================

* master only: GB -> rename

* master only: year 2002, 2016 -> not in silc -> impute 2003/2015
* master only: CH2004+2006, DE2004
* impute FI2004 and NO2014 because of flawed unemp estimates
* rename Greece for post-2004 observations

* rename UK and Greece

replace cntry="GB" if cntry=="UK"
replace cntry="GR" if cntry=="EL"


* use previous years to have complete ESS merge (CH 2004, CH 2006, DE 2004, GB 2004, LU 2004, NL 2004)


preserve
keep if year==2008 & cntry=="CH"
recode year (2008=2004)
save "$final/temp_silc_risk2004CH.dta", replace
restore

preserve
keep if year==2008 & cntry=="CH"
recode year (2008=2006)
save "$final/temp_silc_risk2006CH.dta", replace
restore

preserve
keep if year==2006 & cntry=="DE"
recode year (2006=2004)
save "$final/temp_silc_risk2004DE.dta", replace
restore

preserve
keep if year==2006 & cntry=="GB"
recode year (2006=2004)
save "$final/temp_silc_risk2004GB.dta", replace
restore

preserve
keep if year==2006 & cntry=="NL"
recode year (2006=2004)
save "$final/temp_silc_risk2004NL.dta", replace
restore

* impute FI2004 and NO2014 because of flawed unemp estimates

drop if cntry=="FI"&year==2004
drop if cntry=="NO"&year==2014

preserve
keep if year==2006 & cntry=="FI"
recode year (2006=2004)
save "$final/temp_silc_risk2004FI.dta", replace
restore

preserve
keep if year==2012 & cntry=="NO"
recode year (2012=2014)
save "$final/temp_silc_risk2014NO.dta", replace
restore

append using "$final/temp_silc_risk2004CH.dta"
append using "$final/temp_silc_risk2006CH.dta"
append using "$final/temp_silc_risk2004DE.dta"
append using "$final/temp_silc_risk2004GB.dta"
append using "$final/temp_silc_risk2004NL.dta"


append using "$final/temp_silc_risk2004FI.dta"
append using "$final/temp_silc_risk2014NO.dta"

* use previous years to have complete ESS merge (all 2002, all 2016)

preserve
keep if year==2004
recode year (2004=2002)
save "$final/temp_silc_risk2002.dta", replace
restore

preserve
keep if year==2014
recode year (2014=2016)
save "$final/temp_silc_risk2016.dta", replace
restore

append using "$final/temp_silc_risk2002.dta"
append using "$final/temp_silc_risk2016.dta"

* imputation dummy for robustness
gen imputed = 0
replace imputed = 1 if year==2002
replace imputed = 1 if year==2016
replace imputed = 1 if year==2004&cntry=="CH"
replace imputed = 1 if year==2006&cntry=="CH"
replace imputed = 1 if year==2004&cntry=="DE"
replace imputed = 1 if year==2004&cntry=="GB"
replace imputed = 1 if year==2004&cntry=="NL"
replace imputed = 1 if year==2004&cntry=="FI"
replace imputed = 1 if year==2014&cntry=="NO"

* save full risk file

save "$final/risk_full.dta", replace


* save separate files to merge with ESS

preserve
keep cntry year class8 group unemprisk
bysort cntry year group: keep if _n == 1
drop if group==.
save "$final/unemprisk_group.dta", replace
restore

preserve
keep cntry year isco88_1d isco88group unemprisk_isco88
bysort cntry year isco88group: keep if _n == 1
drop if isco88group==.
save "$final/unemprisk_isco88group.dta", replace
restore

preserve
keep cntry year isco08_1d isco08group unemprisk_isco08
bysort cntry year isco08group: keep if _n == 1
drop if isco08group==.
save "$final/unemprisk_isco08group.dta", replace
restore

preserve
keep cntry year isco88_2d unempisco88_2d unempisco88_2d_robust
bysort cntry year isco88_2d: keep if _n == 1
drop if isco88_2d==.
save "$final/unemprisk_isco88_2d.dta", replace
restore

preserve
keep cntry year isco08_2d unempisco08_2d unempisco08_2d_robust
bysort cntry year isco08_2d: keep if _n == 1
drop if isco08_2d==.
save "$final/unemprisk_isco08_2d.dta", replace
restore

preserve
keep cntry year isco88_1d unempisco88_1d
bysort cntry year isco88_1d: keep if _n == 1
drop if isco88_1d==.
save "$final/unemprisk_isco88_1d.dta", replace
restore

preserve
keep cntry year isco08_1d unempisco08_1d
bysort cntry year isco08_1d: keep if _n == 1
drop if isco08_1d==.
save "$final/unemprisk_isco08_1d.dta", replace
restore

preserve
keep cntry year isco88female unempisco88female
bysort cntry year isco88female: keep if _n == 1
drop if isco88female==.
save "$final/unemprisk_isco88female.dta", replace
restore

preserve
keep cntry year isco08female unempisco08female
bysort cntry year isco08female: keep if _n == 1
drop if isco08female==.
save "$final/unemprisk_isco08female.dta", replace
restore

rename * p*
rename pcntry cntry
rename pyear year
drop *cy

local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), " " , "", .)
display "`date_string'"

preserve
keep cntry year pclass8 pgroup punemprisk
bysort cntry year pgroup: keep if _n == 1
drop if pgroup==.
save "$final/punemprisk_pgroup.dta", replace
restore

preserve
keep cntry year pisco88_1d pisco88group punemprisk_isco88
bysort cntry year pisco88group: keep if _n == 1
drop if pisco88group==.
save "$final/punemprisk_pisco88group.dta", replace
restore

preserve
keep cntry year pisco08_1d pisco08group punemprisk_isco08
bysort cntry year pisco08group: keep if _n == 1
drop if pisco08group==.
save "$final/punemprisk_pisco08group.dta", replace
restore

preserve
keep cntry year pisco88_2d punempisco88_2d punempisco88_2d_robust
bysort cntry year pisco88_2d: keep if _n == 1
drop if pisco88_2d==.
save "$final/punemprisk_pisco88_2d.dta", replace
restore

preserve
keep cntry year pisco08_2d punempisco08_2d punempisco08_2d_robust
bysort cntry year pisco08_2d: keep if _n == 1
drop if pisco08_2d==.
save "$final/punemprisk_pisco08_2d.dta", replace
restore

preserve
keep cntry year pisco88_1d punempisco88_1d
bysort cntry year pisco88_1d: keep if _n == 1
drop if pisco88_1d==.
save "$final/punemprisk_pisco88_1d.dta", replace
restore

preserve
keep cntry year pisco08_1d punempisco08_1d
bysort cntry year pisco08_1d: keep if _n == 1
drop if pisco08_1d==.
save "$final/punemprisk_pisco08_1d.dta", replace
restore

preserve
keep cntry year pisco88female punempisco88female
bysort cntry year pisco88female: keep if _n == 1
drop if pisco88female==.
save "$final/punemprisk_pisco88female.dta", replace
restore

preserve
keep cntry year pisco08female punempisco08female
bysort cntry year pisco08female: keep if _n == 1
drop if pisco08female==.
save "$final/punemprisk_pisco08female.dta", replace
restore

